<template>
  <div class="tag-manage w h box-b p10 b-f">
    <div class="box-b h">
      <div class="header f-b a-i-c">
        <div class="category-title">
          <span class="m-r10">
            标签组管理
          </span>
          <el-button type="danger" icon="plus" @click="editTagGroup()">增加</el-button>
        </div>
        <div class="f-e">
          <el-input v-model="tagName" placeholder="请输入标签组名称" clearable></el-input>
          <el-button type="primary" class="m-l4" icon="search" @click="getDataList">查询</el-button>
        </div>
      </div>
      <div class="table-list" style="height:calc(100% - 70px)">
        <el-table height="100%" :data="tagList" v-loading="dataLoading" stripe border>
          <el-table-column label="标签组名称" prop="name"></el-table-column>
          <el-table-column label="标签组描述" prop="info"></el-table-column>
          <!-- <el-table-column label="标签颜色" prop="color">
            <template #default="{row}">
              <div class="w-20 h-20" :style="{background:row.color}"></div>
            </template>
          </el-table-column> -->
          <el-table-column label="创建时间" prop="createTime"></el-table-column>
          <el-table-column label="修改时间" prop="updateTime"></el-table-column>
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="tagManage(row)">标签管理</el-button>
              <el-button type="text" @click="editTagGroup(row)">编辑</el-button>
              <el-button type="text" @click="deleteTagGroup(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="w f-e m-t5">
        <el-pagination small background v-model:current-page="current" :page-size="size"
          layout="prev, pager, next" @size-change="pageSizeHandler"
          @current-change="pageCurrentHandler" :total="total" class="mt-4" />
      </div>
    </div>

  </div>
</template>
<script lang="ts">
import { TagGroup, type TagGroupInfo } from '@/apis/tag/index'
import { RequestCode } from '@/server/base'
import frameProperty from '@/mixins/frame-property'

export default {
  name: 'TagManage',
  mixins: [frameProperty],
  data() {
    return {
      tagList: [] as TagGroupInfo[],
      total: 0 as number,
      size: 20 as number,
      current: 1 as number,
      dataLoading: false as boolean,

      tagName: '' as string
    }
  },
  methods: {
    /**
     * 获取数据
     */
    getDataList() {
      this.dataLoading = true
      TagGroup.list({
        current: this.current,
        size: this.size,
        keyword: this.tagName
      })
        .then((res: RequestCode) => {
          this.dataLoading = false
          const { records, current, size, total } = res.data
          this.tagList = records
          this.current = current
          this.size = size
          this.total = total
        })
        .catch(error => {
          this.dataLoading = false
        })
    },
    pageSizeHandler(value: number) {
      this.size = value
      this.current = 1
      this.getDataList()
    },
    pageCurrentHandler(value: number) {
      this.current = value
      this.getDataList()
    },

    tagManage(row: TagGroupInfo) {
      // this.dialogPagePush({ name: 'tag', params: { value: this.getParamsString(row) } })
      this.$router.push({ name: 'tag', params: { value: this.getParamsString(row) } })
    },
    editTagGroup(row?: { id: string | number }) {
      const isEdit = !row || !row.id ? this.urlPathCode.FALSE : this.urlPathCode.TRUE
      this.dialogPagePush(
        /*this.$router.push(*/ {
          name: 'tagGroupEdit',
          params: { isEdit, value: this.getParamsString(row || null) }
        }
      )
    },
    deleteTagGroup(row: { id: string | number }) {
      this.$confirm('确定删除该标签吗?', '警告')
        .then(() => {
          TagGroup.removeTagGroupById(row)
            .then((res: RequestCode) => {
              this.showMessage({ type: 'success', message: '删除成功！' })
              this.getDataList()
            })
            .catch((error: any) => {})
        })
        .catch(() => {})
    }
  },
  created() {
    this.getDataList()
  }
}
</script>
<style lang="scss" scoped></style>